package com.zj.core.activity

import android.os.Bundle
import android.text.InputFilter
import android.view.View
import android.widget.LinearLayout
import com.fileutils.SPUtils
import com.systemutils.SoftKeyboardUtils
import com.dialogutils.ToastUtil
import com.zj.core.R
import com.zj.core.base.TitleBarActivity
import com.utils.InputFilterMinMax
import android.view.inputmethod.EditorInfo
import com.fileutils.BaseConstant.PATTERN_IPS
import com.jniutils.DataCollUtil
import com.quitutils.SwipeBackLayout
import com.zj.core.ui.ConfigActivityUi
import com.zj.core.net.RetrofitClient
import com.zj.core.tools.Constant.Companion.BASE_HOST
import com.zj.core.tools.Constant.Companion.BASE_IP
import com.zj.core.tools.Constant.Companion.BASE_SYMBOL
import com.zj.core.tools.Constant.Companion.BASE_URL_HEARD
import com.zj.core.tools.Constant.Companion.BASE_WHOLE_URL
import org.jetbrains.anko.setContentView


class ConfigActivity : TitleBarActivity(), View.OnClickListener {
    lateinit var configUi: ConfigActivityUi

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setQuitMode(SwipeBackLayout.EDGE_LEFT)
    }

    override fun onCreate(groupView: LinearLayout): View? {
        configUi= ConfigActivityUi(groupView)
        return configUi.setContentView(this)
    }

    override fun initView(){
        setTitleText("配置")
        with(configUi){
            this.save_bu_click.setOnClickListener(this@ConfigActivity)
            this.ip_text_tv.inputType = EditorInfo.TYPE_CLASS_PHONE
            this.port_text_tv.filters = arrayOf<InputFilter>(InputFilterMinMax("0", "65535"))
        }
        loadIpConfig()
    }

    private fun loadIpConfig() {
        with(configUi){
            var mBaseUrl: String = SPUtils.get(this@ConfigActivity,BASE_WHOLE_URL, "") as String
            if (mBaseUrl != "") {
                if (mBaseUrl.contains(BASE_URL_HEARD)) {
                    mBaseUrl = mBaseUrl.replace(BASE_URL_HEARD, "")
                }
                if (mBaseUrl.contains(BASE_SYMBOL)){
                    val mUrlStr = mBaseUrl.split(BASE_SYMBOL.toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                    this.ip_text_tv.setText(mUrlStr[0])
                    if(mUrlStr.size>1){
                        this.port_text_tv.setText(mUrlStr[1])
                    }
                } else {
                    this.ip_text_tv.setText(mBaseUrl)
                }
            } else {
                this.ip_text_tv.setText(BASE_IP)
                this.port_text_tv.setText(BASE_HOST)
            }
        }
    }

    override fun onClick(v: View?) {
        super.onClick(v)

        val mBaseUrl: String
        when (v?.id) {
            R.id.save_bu_click -> {
                if (SoftKeyboardUtils.isSoftShowing(this)) {
                    SoftKeyboardUtils.hideSystemSoftKeyboard(this,currentFocus.windowToken)
                }

                DataCollUtil.patternValue(PATTERN_IPS,configUi.ip_text_tv.text.toString())?.let {
                    if(it.isNotBlank()){
                        if (!configUi.port_text_tv.text.equals("")){
                            mBaseUrl = configUi.ip_text_tv.text.toString() + BASE_SYMBOL + configUi.port_text_tv.text.toString()
                        } else {
                            mBaseUrl = configUi.ip_text_tv.text.toString()
                        }
                        SPUtils.put(this, BASE_WHOLE_URL, mBaseUrl)

                        ToastUtil.showShort(this, "修改成功")
                        RetrofitClient.retrofit=null
                        setResult(RES_CODE_CONFIG)
                        finish()
                    }else{
                        ToastUtil.showShort(this, "请输入正确的IP地址")
                    }
                }?:let{
                    ToastUtil.showShort(this, "请输入正确的IP地址")
                }
            }
        }
    }

    companion object {
        val RES_CODE_CONFIG=3
    }
}